// pages/order/order.js
Page({
	offsetTopList: [],
	onSideBarChange(e) {
		const { value } = e.detail;
		this.setData({ sideBarIndex: value, scrollTop: this.offsetTopList[value] });
	},
	onScroll(e) {
		const { scrollTop } = e.detail;
		const threshold = 50; // 下一个标题与顶部的距离

		if (scrollTop < threshold) {
			this.setData({ sideBarIndex: 0 });
			return;
		}

		const index = this.offsetTopList.findIndex((top) => top > scrollTop && top - scrollTop <= threshold);

		if (index > -1) {
			this.setData({ sideBarIndex: index });
		}
	},

	onGoodVisibleChange(e) {
		this.setData({
			goodVisible: e.detail.visible,
		});
	},

	onCardVisibleChange(e) {
		this.setData({
			cardVisible: e.detail.visible,
		});
	},

	onSelectedChange(e){
		const specIndex = e.currentTarget.dataset.specIndex; // 获取规格索引
		const selectedIndex = e.detail.value; // 获取选中的选项索引
		// 使用setData更新对应规格的selectedIndex
		this.setData({
			[`good.specification[${specIndex}].selectedIndex`]: selectedIndex
		});
		const specifications = this.data.good.specification;
		const total = specifications.reduce((sum, spec) => {
			const selectedOption = spec.options[spec.selectedIndex];
			return sum + Number(selectedOption.additional_price);
		}, Number(this.data.good.price));

		// 生成选项描述
		const optionsDesc = specifications.map(spec => {
			return spec.options[spec.selectedIndex].title;
		}).join('、 ');

		this.setData({
			[`good.selectedPrice`]: total,
			[`good.totalPrice`]: total*this.data.good.selectedNum,
			[`good.selectedOptions`]: optionsDesc
		});
		console.log(this.data.good);
	},

	onGoodsCardTap(e){
		// 获取点击商品索引
		const goodIndex = e.currentTarget.dataset.goodIndex;
		// 获取当前侧边栏选中分类索引
		const categoryIndex = e.currentTarget.dataset.categoryIndex;
		// 通过双索引定位具体商品
		const selectedGood = this.data.categories[categoryIndex].goodCard[goodIndex];

		const initialSpecs = selectedGood.specification;
		const initialPrice = initialSpecs.reduce((sum, spec) => {
			return sum + Number(spec.options[0].additional_price);
		}, Number(selectedGood.price));

		const newGood = {
			...selectedGood,
			selectedNum: 1,
			selectedPrice: initialPrice,
			totalPrice: initialPrice,
			selectedOptions: initialSpecs.map(s => s.options[0].title).join('、 ')
		};

		this.setData({
			goodVisible: true,
			good: newGood
		});
	},
	onGoodStepperChange(e){
		const { value } = e.detail
		this.setData({
			[`good.selectedNum`]: value,
			[`good.totalPrice`]: value * this.data.good.selectedPrice
		});
	},
	onAddCartTap(e){
		const newItem = {
			...this.data.good,
		};
		this.setData({
			milkTeaCart: {
				// 累加总数量
				totalNum: this.data.milkTeaCart.totalNum + newItem.selectedNum,
				// 累加总价格（需确保selectedPrice是数值类型）
				totalPrice: (Number(this.data.milkTeaCart.totalPrice) +
					Number(newItem.selectedPrice) * newItem.selectedNum),
				// 添加新商品到数组
				milkTea: [...this.data.milkTeaCart.milkTea, newItem]
			},
			goodVisible: false
		})
		console.log(this.data.milkTeaCart);
	},
	onMilkTeaCartTap(e){
		if (this.data.milkTeaCart.totalNum > 0) {
			this.setData({
				cardVisible: true,
			});
		}
	},
	onClearTap(e){
		this.setData({
			milkTeaCart: {
				totalNum: 0,
				totalPrice: 0,
				milkTea: []
			},
			cardVisible: false
		});
	},
	onCartStepperChange(e){
		const index = e.currentTarget.dataset.milkTeaIndex; // 获取商品索引
		const value = e.detail.value; // 获取新数值
		const milkTea = this.data.milkTeaCart.milkTea;

		// 创建新数组避免直接修改原数据
		const newMilkTea = [...milkTea];
		const changedItem = {...newMilkTea[index]};
		// 计算价格差异
		const priceDiff = (value - changedItem.selectedNum) * changedItem.selectedPrice;

		if (value ===0) {
			// 删除商品逻辑
			newMilkTea.splice(index, 1); // 从数组中移除该商品
			this.setData({
				milkTeaCart: {
					totalNum: this.data.milkTeaCart.totalNum - changedItem.selectedNum,
					totalPrice: Number((this.data.milkTeaCart.totalPrice - changedItem.totalPrice).toFixed(2)),
					milkTea: newMilkTea
				}
			});
		}
		else {
			// 更新商品数量
			changedItem.selectedNum = value;
			changedItem.totalPrice = value * changedItem.selectedPrice;

			// 更新数组
			newMilkTea[index] = changedItem;

			this.setData({
				milkTeaCart: {
					totalNum: Math.max(this.data.milkTeaCart.totalNum + (value - changedItem.selectedNum), 0),
					totalPrice: Number((Number(this.data.milkTeaCart.totalPrice) + priceDiff).toFixed(2)),
					milkTea: newMilkTea
				}
			});
		}

		if (this.data.milkTeaCart.totalNum === 0) {
			this.setData({
				cardVisible: false,
			});
		}
	},
	onMilkTeaOrderSettlementTap(e){
		if (this.data.milkTeaCart.totalNum === 0) {
			wx.showToast({ title: '请先添加商品', icon: 'none' });
			return;
		}
		getApp().globalData.milkTeaOrder = this.data.milkTeaCart;

		wx.navigateTo({
			url: '/pages/milkTeaOrderSettlement/milkTeaOrderSettlement',
		});
	},
	/**
	 * 页面的初始数据
	 */
	data: {
		sideBarIndex: 0,
		scrollTop: 0,
		addressHeight: 0,
		orderShoppingCartHeight: 0,
		goodVisible: false,
		cardVisible: false,

		milkTeaCart:{
			totalNum: 0,
			totalPrice: 0,
			milkTea: []
		},
		good:{},

		categories: [
			{
				label: '选项一',
				title: '标题一',
				icon: 'app',
				badgeProps: {},
				goodCard:[
					{
						id: 1,
						title: '标题',
						content: '内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容',
						img: 'https://tdesign.gtimg.com/mobile/demos/swiper1.png',
						price: 100,
						specification:[
							{
								name: '规格',
								selectedIndex:0,
								options:[
									{
										title: '大大大大大大大大大大大大大大大大大大',
										additional_price: 120.1,
									},
									{
										title: '中',
										additional_price: 110
									},
									{
										title: "小",
										additional_price: 100
									}
								]
							},
							{
								name: '状态',
								selectedIndex:0,
								options:[
									{
										title: '热',
										additional_price: 0,
									},
									{
										title: '温',
										additional_price: 0
									},
									{
										title: "冰",
										additional_price: 0
									}
								]
							},
							{
								name: '甜度',
								selectedIndex:0,
								options:[
									{
										title: '标准',
										additional_price: 0
									},
									{
										title: '少糖',
										additional_price: 0
									},
									{
										title: "不加糖",
										additional_price: 0
									}
								]
							}
						],
					},
					{
						id: 2,
						title: '标题',
						content: '内容',
						img: 'https://tdesign.gtimg.com/mobile/demos/swiper1.png',
						price: 100,
						specification:[
							{
								name: '规格',
								selectedIndex:0,
								options:[
									{
										title: '大',
										additional_price: 120,
									},
									{
										title: '中',
										additional_price: 110
									},
									{
										title: "小",
										additional_price: 100
									}
								]
							},
							{
								name: '状态',
								selectedIndex:0,
								options:[
									{
										title: '热',
										additional_price: 0,
									},
									{
										title: '温',
										additional_price: 0
									},
									{
										title: "冰",
										additional_price: 0
									}
								]
							},
							{
								name: '甜度',
								selectedIndex:0,
								options:[
									{
										title: '标准',
										additional_price: 0
									},
									{
										title: '少糖',
										additional_price: 0
									},
									{
										title: "不加糖",
										additional_price: 0
									}
								]
							}
						],
					},
					{
						id: 3,
						title: '标题',
						content: '内容',
						img: 'https://tdesign.gtimg.com/mobile/demos/swiper1.png',
						price: 100,
						specification:[
							{
								name: '规格',
								selectedIndex:0,
								options:[
									{
										title: '大',
										additional_price: 120,
									},
									{
										title: '中',
										additional_price: 110
									},
									{
										title: "小",
										additional_price: 100
									}
								]
							},
							{
								name: '状态',
								selectedIndex:0,
								options:[
									{
										title: '热',
										additional_price: 0,
									},
									{
										title: '温',
										additional_price: 0
									},
									{
										title: "冰",
										additional_price: 0
									}
								]
							},
							{
								name: '甜度',
								selectedIndex:0,
								options:[
									{
										title: '标准',
										additional_price: 0
									},
									{
										title: '少糖',
										additional_price: 0
									},
									{
										title: "不加糖",
										additional_price: 0
									}
								]
							}
						],
					}
				]
			},
			{
				label: '选项二',
				title: '标题二',
				icon: 'app',
				badgeProps: {
					dot: true,
				},
				goodCard: [
					{
						id: 4,
						title: '标题',
						content: '内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容',
						img: 'https://tdesign.gtimg.com/mobile/demos/swiper1.png',
						price: 100,

						specification:[
							{
								name: '规格',
								selectedIndex:0,
								options:[
									{
										title: '大',
										additional_price: 120,
									},
									{
										title: '中',
										additional_price: 110
									},
									{
										title: "小",
										additional_price: 100
									}
								]
							},
							{
								name: '状态',
								selectedIndex:0,
								options:[
									{
										title: '热',
										additional_price: 0,
									},
									{
										title: '温',
										additional_price: 0
									},
									{
										title: "冰",
										additional_price: 0
									}
								]
							},
							{
								name: '甜度',
								selectedIndex:0,
								options:[
									{
										title: '标准',
										additional_price: 0
									},
									{
										title: '少糖',
										additional_price: 0
									},
									{
										title: "不加糖",
										additional_price: 0
									}
								]
							}
						],
					},
					{
						id: 5,
						title: '标题',
						content: '内容',
						img: 'https://tdesign.gtimg.com/mobile/demos/swiper1.png',
						price: 100,

						specification:[
							{
								name: '规格',
								selectedIndex:0,
								options:[
									{
										title: '大',
										additional_price: 120,
									},
									{
										title: '中',
										additional_price: 110
									},
									{
										title: "小",
										additional_price: 100
									}
								]
							},
							{
								name: '状态',
								selectedIndex:0,
								options:[
									{
										title: '热',
										additional_price: 0,
									},
									{
										title: '温',
										additional_price: 0
									},
									{
										title: "冰",
										additional_price: 0
									}
								]
							},
							{
								name: '甜度',
								selectedIndex:0,
								options:[
									{
										title: '标准',
										additional_price: 0
									},
									{
										title: '少糖',
										additional_price: 0
									},
									{
										title: "不加糖",
										additional_price: 0
									}
								]
							}
						],
					},
					{
						id: 6,
						title: '标题',
						content: '内容',
						img: 'https://tdesign.gtimg.com/mobile/demos/swiper1.png',
						price: 100,

						specification:[
							{
								name: '规格',
								selectedIndex:0,
								options:[
									{
										title: '大',
										additional_price: 120,
									},
									{
										title: '中',
										additional_price: 110
									},
									{
										title: "小",
										additional_price: 100
									}
								]
							},
							{
								name: '状态',
								selectedIndex:0,
								options:[
									{
										title: '热',
										additional_price: 0,
									},
									{
										title: '温',
										additional_price: 0
									},
									{
										title: "冰",
										additional_price: 0
									}
								]
							},
							{
								name: '甜度',
								selectedIndex:0,
								options:[
									{
										title: '标准',
										additional_price: 0
									},
									{
										title: '少糖',
										additional_price: 0
									},
									{
										title: "不加糖",
										additional_price: 0
									}
								]
							}
						],
					}
				]
			},
			{
				label: '选项三',
				title: '标题三',
				icon: 'app',
				badgeProps: {},
				goodCard: [
					{
						id: 7,
						title: '标题',
						content: '内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容',
						img: 'https://tdesign.gtimg.com/mobile/demos/swiper1.png',
						price: 100,

						specification:[
							{
								name: '规格',
								selectedIndex:0,
								options:[
									{
										title: '大',
										additional_price: 120,
									},
									{
										title: '中',
										additional_price: 110
									},
									{
										title: "小",
										additional_price: 100
									}
								]
							},
							{
								name: '状态',
								selectedIndex:0,
								options:[
									{
										title: '热',
										additional_price: 0,
									},
									{
										title: '温',
										additional_price: 0
									},
									{
										title: "冰",
										additional_price: 0
									}
								]
							},
							{
								name: '甜度',
								selectedIndex:0,
								options:[
									{
										title: '标准',
										additional_price: 0
									},
									{
										title: '少糖',
										additional_price: 0
									},
									{
										title: "不加糖",
										additional_price: 0
									}
								]
							}
						],
					},
					{
						id: 8,
						title: '标题',
						content: '内容',
						img: 'https://tdesign.gtimg.com/mobile/demos/swiper1.png',
						price: 100,

						specification:[
							{
								name: '规格',
								selectedIndex:0,
								options:[
									{
										title: '大',
										additional_price: 120,
									},
									{
										title: '中',
										additional_price: 110
									},
									{
										title: "小",
										additional_price: 100
									}
								]
							},
							{
								name: '状态',
								selectedIndex:0,
								options:[
									{
										title: '热',
										additional_price: 0,
									},
									{
										title: '温',
										additional_price: 0
									},
									{
										title: "冰",
										additional_price: 0
									}
								]
							},
							{
								name: '甜度',
								selectedIndex:0,
								options:[
									{
										title: '标准',
										additional_price: 0
									},
									{
										title: '少糖',
										additional_price: 0
									},
									{
										title: "不加糖",
										additional_price: 0
									}
								]
							}
						],
					},
					{
						id: 9,
						title: '标题',
						content: '内容',
						img: 'https://tdesign.gtimg.com/mobile/demos/swiper1.png',
						price: 100,

						specification:[
							{
								name: '规格',
								selectedIndex:0,
								options:[
									{
										title: '大',
										additional_price: 120,
									},
									{
										title: '中',
										additional_price: 110
									},
									{
										title: "小",
										additional_price: 100
									}
								]
							},
							{
								name: '状态',
								selectedIndex:0,
								options:[
									{
										title: '热',
										additional_price: 0,
									},
									{
										title: '温',
										additional_price: 0
									},
									{
										title: "冰",
										additional_price: 0
									}
								]
							},
							{
								name: '甜度',
								selectedIndex:0,
								options:[
									{
										title: '标准',
										additional_price: 0
									},
									{
										title: '少糖',
										additional_price: 0
									},
									{
										title: "不加糖",
										additional_price: 0
									}
								]
							}
						],
					}
				]
			},
			{
				label: '选项四',
				title: '标题四',
				icon: 'app',
				badgeProps: {
					count: 6,
				},
				goodCard: [
					{
						id: 10,
						title: '标题',
						content: '内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容',
						img: 'https://tdesign.gtimg.com/mobile/demos/swiper1.png',
						price: 100,

						specification:[
							{
								name: '规格',
								selectedIndex:0,
								options:[
									{
										title: '大',
										additional_price: 120,
									},
									{
										title: '中',
										additional_price: 110
									},
									{
										title: "小",
										additional_price: 100
									}
								]
							},
							{
								name: '状态',
								selectedIndex:0,
								options:[
									{
										title: '热',
										additional_price: 0,
									},
									{
										title: '温',
										additional_price: 0
									},
									{
										title: "冰",
										additional_price: 0
									}
								]
							},
							{
								name: '甜度',
								selectedIndex:0,
								options:[
									{
										title: '标准',
										additional_price: 0
									},
									{
										title: '少糖',
										additional_price: 0
									},
									{
										title: "不加糖",
										additional_price: 0
									}
								]
							}
						],
					},
					{
						id: 11,
						title: '标题',
						content: '内容',
						img: 'https://tdesign.gtimg.com/mobile/demos/swiper1.png',
						price: 100,

						specification:[
							{
								name: '规格',
								selectedIndex:0,
								options:[
									{
										title: '大',
										additional_price: 120,
									},
									{
										title: '中',
										additional_price: 110
									},
									{
										title: "小",
										additional_price: 100
									}
								]
							},
							{
								name: '状态',
								selectedIndex:0,
								options:[
									{
										title: '热',
										additional_price: 0,
									},
									{
										title: '温',
										additional_price: 0
									},
									{
										title: "冰",
										additional_price: 0
									}
								]
							},
							{
								name: '甜度',
								selectedIndex:0,
								options:[
									{
										title: '标准',
										additional_price: 0
									},
									{
										title: '少糖',
										additional_price: 0
									},
									{
										title: "不加糖",
										additional_price: 0
									}
								]
							}
						],
					},
					{
						id: 12,
						title: '标题',
						content: '内容',
						img: 'https://tdesign.gtimg.com/mobile/demos/swiper1.png',
						price: 100,

						specification:[
							{
								name: '规格',
								selectedIndex:0,
								options:[
									{
										title: '大',
										additional_price: 120,
									},
									{
										title: '中',
										additional_price: 110
									},
									{
										title: "小",
										additional_price: 100
									}
								]
							},
							{
								name: '状态',
								selectedIndex:0,
								options:[
									{
										title: '热',
										additional_price: 0,
									},
									{
										title: '温',
										additional_price: 0
									},
									{
										title: "冰",
										additional_price: 0
									}
								]
							},
							{
								name: '甜度',
								selectedIndex:0,
								options:[
									{
										title: '标准',
										additional_price: 0
									},
									{
										title: '少糖',
										additional_price: 0
									},
									{
										title: "不加糖",
										additional_price: 0
									}
								]
							}
						],
					}
				]
			},
			{
				label: '选项五',
				title: '标题五',
				icon: 'app',
				badgeProps: {},
				goodCard: [
					{
						id: 13,
						title: '标题',
						content: '内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容',
						img: 'https://tdesign.gtimg.com/mobile/demos/swiper1.png',
						price: 100,

						specification:[
							{
								name: '规格',
								selectedIndex:0,
								options:[
									{
										title: '大',
										additional_price: 120,
									},
									{
										title: '中',
										additional_price: 110
									},
									{
										title: "小",
										additional_price: 100
									}
								]
							},
							{
								name: '状态',
								selectedIndex:0,
								options:[
									{
										title: '热',
										additional_price: 0,
									},
									{
										title: '温',
										additional_price: 0
									},
									{
										title: "冰",
										additional_price: 0
									}
								]
							},
							{
								name: '甜度',
								selectedIndex:0,
								options:[
									{
										title: '标准',
										additional_price: 0
									},
									{
										title: '少糖',
										additional_price: 0
									},
									{
										title: "不加糖",
										additional_price: 0
									}
								]
							}
						],
					},
					{
						id: 14,
						title: '标题',
						content: '内容',
						img: 'https://tdesign.gtimg.com/mobile/demos/swiper1.png',
						price: 100,

						specification:[
							{
								name: '规格',
								selectedIndex:0,
								options:[
									{
										title: '大',
										additional_price: 120,
									},
									{
										title: '中',
										additional_price: 110
									},
									{
										title: "小",
										additional_price: 100
									}
								]
							},
							{
								name: '状态',
								selectedIndex:0,
								options:[
									{
										title: '热',
										additional_price: 0,
									},
									{
										title: '温',
										additional_price: 0
									},
									{
										title: "冰",
										additional_price: 0
									}
								]
							},
							{
								name: '甜度',
								selectedIndex:0,
								options:[
									{
										title: '标准',
										additional_price: 0
									},
									{
										title: '少糖',
										additional_price: 0
									},
									{
										title: "不加糖",
										additional_price: 0
									}
								]
							}
						],
					},
					{
						id: 15,
						title: '标题',
						content: '内容',
						img: 'https://tdesign.gtimg.com/mobile/demos/swiper1.png',
						price: 100,

						specification:[
							{
								name: '规格',
								selectedIndex:0,
								options:[
									{
										title: '大',
										additional_price: 120,
									},
									{
										title: '中',
										additional_price: 110
									},
									{
										title: "小",
										additional_price: 100
									}
								]
							},
							{
								name: '状态',
								selectedIndex:0,
								options:[
									{
										title: '热',
										additional_price: 0,
									},
									{
										title: '温',
										additional_price: 0
									},
									{
										title: "冰",
										additional_price: 0
									}
								]
							},
							{
								name: '甜度',
								selectedIndex:0,
								options:[
									{
										title: '标准',
										additional_price: 0
									},
									{
										title: '少糖',
										additional_price: 0
									},
									{
										title: "不加糖",
										additional_price: 0
									}
								]
							}
						],
					}
				]
			},
		],

		banners:[
			{
				id: "1",
				value: "https://tdesign.gtimg.com/mobile/demos/swiper1.png"
			},
			{
				id: "2",
				value: "https://tdesign.gtimg.com/mobile/demos/swiper2.png"
			}
		],
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		console.log(1111)
		const query = wx.createSelectorQuery().in(this);
		const { sideBarIndex } = this.data;
		query.selectAll('.title').boundingClientRect();
		query.select('.address-card').boundingClientRect();
		query.select('.banner-card').boundingClientRect();
		query.select('.milk-tea-cart').boundingClientRect();
		query.exec((res) => {
			const [rects, { height: addressHeight = 0 }, { height: bannerHeight = 0 }, { height: orderShoppingCartHeight = 0 }] = res;
			this.offsetTopList = rects.map((item) => item.top - addressHeight );
			this.offsetTopList[0]=this.offsetTopList[0]-bannerHeight
			this.setData({ addressHeight, scrollTop: this.offsetTopList[sideBarIndex] });
			this.setData({ orderShoppingCartHeight });
		});
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})


// const data =
// 	{
// 		"statusCode": 200,
// 		"message": "success",
// 		"data": {
// 			"goodsDetail":{
// 				"id": 1,
// 				"title": "标题",
// 				"content": "内容",
// 				"img": "'https://tdesign.gtimg.com/mobile/demos/swiper1.png'",
// 				"price": 100,
// 				"specification":[
// 					{
// 						"name": "规格",
// 						"selectedIndex":0,
// 						"options":[
// 							{
// 								"title": "大",
// 								"additional_price": "120.1"
// 							},
// 							{
// 								"title": "中",
// 								"additional_price": "110"
// 							},
// 							{
// 								"title": "小",
// 								"additional_price": "100"
// 							}
// 						]
// 					},
// 					{
// 						"name": "状态",
// 						"selectedIndex":0,
// 						"options":[
// 							{
// 								"title": "热",
// 								"additional_price": 0,
// 							},
// 							{
// 								"title": "温",
// 								"additional_price": 0
// 							},
// 							{
// 								"title": "冰",
// 								"additional_price": 0
// 							}
// 						]
// 					}
// 				]
// 			}
// 		}
// 	}